<?php
/**
 * Created by PhpStorm.
 * User: zsl
 * Date: 2019-10-02
 * Time: 14:30
 */

namespace App\Service;

use App\Entity\Video;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\File\UploadedFile;

class UploadService
{

    /**
     * @var EntityManagerInterface
     */
    private $em;

    /**
     * UploadService constructor.
     * @param EntityManagerInterface $em
     */
    public function __construct(EntityManagerInterface $em)
    {
        $this->em = $em;
    }

    public function uploadFile(UploadedFile $file): string
    {
        $file->getRealPath();
        $filePath = __DIR__ . '/../../var/file/';

        $video = new Video();
        $video->setFileType('mp4');
        $video->setFilePath($file->getPath())
            ->setTime(time());
        $this->em->persist($video);
        $this->em->flush();

        $filePath = $filePath . $video->getId() . '.' . $video->getFileType();
        copy($file->getRealPath(), $filePath);
        return $filePath;
    }

}
